using System;
using System.Collections;
using System.Web.UI;
using System.Web.UI.WebControls;
using Sedna.Core.Controller;
using Sedna.Core.Controller.Configurations;
using Sedna.Core.Controls.Actions.Support;
using Sedna.Core.Controls.Support;
using Sedna.Core.Controls.Support.Javascript;
using Nullables;

namespace Sedna.Core.Controls
{
    [ValidationProperty("Text")]
	public class Enter : BaseEnter,  IClientScriptingControl
	{	
		/// <summary>
		/// Id (int) of selected object. Null, if no object was selected 
		/// using popup. 
		/// </summary>
		public string Value
		{
			get { return ((EnterPopupButton)PopupButton).Value; }
            set { ((EnterPopupButton)PopupButton).Value = value; }
		}

		public override bool ClientScriptingEnabled
		{
			get { return true; }
			set { /*Always enabled*/ }
		}
        
        


		public Enter(IConfiguration configuration) : base(configuration)
		{
		}

		protected override void Build()
		{
			base.Build();
			IClientScriptingConfiguration clientScriptingConfiguration = Configuration as IClientScriptingConfiguration;
			if (clientScriptingConfiguration!=null)
			{
				ClientScriptingEnabled=clientScriptingConfiguration.ClientScriptingEnabled;
				ClientVariable=clientScriptingConfiguration.ClientVariable;
			}

		}

	    protected override BasePopupButton BuildPopupButton()
	    {
            return new EnterPopupButton(((IEnterConfiguration)Configuration).PopupButtonConfiguration);
        }

//		protected override void OnPreRender(EventArgs e)
//		{
//			base.OnPreRender(e); 
//			EnableClientScripting();
//		}
//
//		protected override void EnableClientScripting()
//		{
//			base.EnableClientScripting();
//			popupButton.DisplayControls["display"] = textBox.ClientVariable;
//
//            JavascriptController controller = JavascriptController.Current;
//
//			controller.DeclareClass("Enter");
//            controller.DeclareObject(ClientVariable, "Enter", textBox.ClientVariable, popupButton.ClientVariable);
//		}


	    protected override void OnPreRender(EventArgs e)
	    {
	        base.OnPreRender(e);
	        if (ClientScriptingEnabled)
	        {
                DeclareScriptVariable();
	        }
	    }

	    protected override void DeclareScriptVariable()
	    {
            PopupButton.DisplayControls["display"] = textBox.ClientVariable;
            base.DeclareScriptVariable();
	    }


	    protected override IDictionary BuildScriptParameters()
	    {
	        IDictionary parameters =  base.BuildScriptParameters();
            parameters.Add("textBox", textBox.ClientVariable);
            parameters.Add("popupButton", PopupButton.ClientVariable);
            return parameters;
	    }

	    protected override void Unbind()
		{
			base.Unbind();
            binder.Unbind("Value", Configuration.PopupButtonConfiguration.DataParser);
			binder.Unbind("Text");
		}

		protected override void Bind()
		{
			base.Bind();
			binder.Bind("Text");
            binder.Bind("Value", Configuration.PopupButtonConfiguration.DataTranslator);
		}


	}
}
